import os
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import requests
import json
import sqliteOperation
def gold():
    url = "https://api.pearktrue.cn/api/goldprice/"
    try:
        price=0
        response = requests.request("GET", url)
        response_data = json.loads(response.text)
        for item in response_data["data"]:
            if item["dir"] == "au9999":
                price = item["price"]
                price=price
                break
        print(price)
        return str(price)
    except requests.exceptions.ConnectionError as e:
        return (
            jsonify({"error": "连接错误: " + str(e)}),
            503,
        )  # 503是HTTP状态码，表示服务不可用
    except requests.exceptions.Timeout as e:
        return (
            jsonify({"error": "请求超时: " + str(e)}),
            504,
        )  # 504是HTTP状态码，表示网关超时
    except requests.exceptions.RequestException as e:
        return (
            jsonify({"error": "网关错误: " + str(e)}),
            502,
        )  # 502是HTTP状态码，表示网关错误     


if __name__ == "__main__":
    gold()
    
